package com.fzy.common.redis.impl;

import com.alibaba.fastjson.JSON;
import com.fzy.common.bean.ObjectUtil;
import com.fzy.common.redis.JedisClient;
import com.fzy.common.util.FastJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisCluster;

import java.util.List;
import java.util.Map;


/**
 * @author lishw
 * @create 2017-01-18
 */
public class JedisClientCluster implements JedisClient {

    @Autowired
    private JedisCluster jedis;

    @Override
    public String get(String key) {
        return jedis.get(key);
    }

    @Override
    public String set(String key, String value) {

        return jedis.set(key, value);
    }

    @Override
    public String hget(String hkey, String key) {

        return jedis.hget(key, key);
    }

    @Override
    public long hset(String hkey, String key, String value) {

        return jedis.hset(hkey, key, value);
    }

    @Override
    public long incr(String key) {

        return jedis.incr(key);
    }

    @Override
    public long expire(String key, Integer second) {

        return jedis.expire(key, second);
    }

    @Override
    public long ttl(String key) {

        return jedis.ttl(key);
    }

    @Override
    public long del(String key) {

        return jedis.del(key);
    }

    @Override
    public long hdel(String hkey, String key) {

        return jedis.hdel(hkey, key);
    }


    @Override
    public String setObject(String key, Object object) {
        return jedis.set(key.getBytes(), ObjectUtil.serialize(object));
    }

    @Override
    public <T> T getObject(String key) {
        byte[] bytes = jedis.get(key.getBytes());
        return ObjectUtil.unserialize(bytes);
    }

    @Override
    public String setJson(String key, Object object) {
        return jedis.set(key, FastJsonUtil.toJson(object));
    }

    @Override
    public <T> T getJson(String key, Class<T> clazz) {
        String data = jedis.get(key);
        return JSON.parseObject(data, clazz);
    }

    /**
     * 同时将多个field - value(域-值)对设置到哈希表key中
     *
     * @param key
     * @param hash
     * @return
     * @author 冯赵杨
     * @create 2018/9/7
     */
    @Override
    public String hmset(String key, Map<String, String> hash) {
        return jedis.hmset(key, hash);
    }

    /**
     * 返回哈希表key中，一个或多个给定域的值
     *
     * @param key
     * @param fields
     * @return
     * @author 冯赵杨
     * @create 2018/9/7
     */
    @Override
    public List<String> hmget(String key, String... fields) {
        return jedis.hmget(key, fields);
    }

    /**
     * 返回哈希表key中，所有的域和值
     *
     * @param key
     * @return
     * @author 冯赵杨
     * @create 2018/9/7
     */
    @Override
    public Map<String, String> hgetAll(String key) {
        return jedis.hgetAll(key);
    }
}
